/*
 * Copyright 2010 (c) Dou Yongwang (douyongwang@gmail.com)
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */


#include "FacebookObject.h"
#include <sjson.h>
using namespace sjson;

struct CFacebookObject::PImpl
	{
	TAny* operator new(TUint aSize) {return User::AllocZ(aSize);}
	TAny* operator new(TUint aSize, TLeave) {return User::AllocZL(aSize);}
//	JValue* iValue;		// not owned
	CJSONObject* iObject;	// not owned
	};

CFacebookObject::CFacebookObject()
	{
	// No implementation required
	}

CFacebookObject::~CFacebookObject()
	{
	delete iPimpl;
	}

EXPORT_C
CFacebookObject* CFacebookObject::NewLC(TAny* aJObjectObject)
	{
	CFacebookObject* self = new (ELeave) CFacebookObject();
	CleanupStack::PushL(self);
	self->ConstructL(aJObjectObject);
	return self;
	}

EXPORT_C
CFacebookObject* CFacebookObject::NewL(TAny* aJObjectObject)
	{
	CFacebookObject* self = CFacebookObject::NewLC(aJObjectObject);
	CleanupStack::Pop(); // self;
	return self;
	}

void CFacebookObject::ConstructL(TAny* aJObjectObject)
	{
//	if (aJObjectObject == NULL)
//		User::Leave(KErrArgument);
	iPimpl = new (ELeave) PImpl();
	this->SetJSONObject(aJObjectObject);
//	iPimpl->iObject = static_cast<JObject*>(aJObjectObject);
	}

void CFacebookObject::SetJSONObject(TAny* aJONObject)
	{
	iPimpl->iObject = static_cast<CJSONObject*>(aJONObject);
	}

//TInt CFacebookObject::getInt32(const TDesC8& aField, TInt32& aValue) const
//	{
//	if (iPimpl->iObject->Has<TInt32>(aField))
//		{
//		aValue = iPimpl->iObject->Get<TInt32>(aField);
//		return KErrNone;
//		}
//	return KErrNotFound;
//	}

EXPORT_C
TInt CFacebookObject::getInt64(const TDesC8& aField, TInt64& aValue) const
	{
	if (iPimpl->iObject->Has<TInt64>(aField))
		{
		aValue = iPimpl->iObject->Get<TInt64>(aField);
		return KErrNone;
		}
	return KErrNotFound;
	}

EXPORT_C
TInt CFacebookObject::getBool(const TDesC8& aField, TBool& aValue) const
	{
	if (iPimpl->iObject->Has<TBool>(aField))
		{
		aValue = iPimpl->iObject->Get<TBool>(aField);
		return KErrNone;
		}
	return KErrNotFound;
	}
EXPORT_C
TInt CFacebookObject::getUtf8String(const TDesC8& aField, TPtrC8& aValue) const
	{
	if (iPimpl->iObject->Has<TDesC8>(aField))
		{
		const TDesC8& val = iPimpl->iObject->Get<TDesC8>(aField);
		aValue.Set(val);
		return KErrNone;
		}
	return KErrNotFound;
	}
EXPORT_C
TInt CFacebookObject::getArray(const TDesC8& /*aField*/, RFacebookObjectArray& /*aValue*/) const
	{
	return KErrNotSupported;
	}
EXPORT_C
TInt CFacebookObject::getObject(const TDesC8& aField, CFacebookObject& aValue) const
	{
	if (iPimpl->iObject->Has<CJSONObject>(aField))
		{
		CJSONObject& val = iPimpl->iObject->Get<CJSONObject>(aField);
		aValue.SetJSONObject(&val);
		return KErrNone;
		}
	return KErrNotFound;
	}
